package com.wangzhen.spring.ioc.lifecycle.initializing;

import com.wangzhen.spring.ioc.dependcy.entity.Terminate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 自定义BeanPostProcessor，研究postProcessBeforeInitialization、postProcessAfterInitialization
 * 这两个方法的调用时机
 */
public class MyBeanPostProcessor implements BeanPostProcessor {


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("terminate".equals(beanName) && Terminate.class == bean.getClass()) {
            Terminate terminate = Terminate.class.cast(bean);
            terminate.setWhoAmI("whoAmI 111 from postProcessBeforeInitialization method");
            System.out.println(terminate);
            return terminate;
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("terminate".equals(beanName) && Terminate.class == bean.getClass()) {
            Terminate terminate = Terminate.class.cast(bean);
            terminate.setWhoAmI("whoAmI 333 from postProcessAfterInitialization method");
            System.out.println(terminate);
            return terminate;
        }
        return bean;
    }
}
